package com.upcontrol.uartagent.core;

import com.csjbot.csjbase.log.Csjlogger;
import com.smatek.uart.UartComm;
import com.upcontrol.uartagent.core.inter.IConnector;
import com.upcontrol.uartagent.core.inter.DataReceive;

import java.util.Arrays;

/**
 * Copyright (c) 2017, SuZhou CsjBot. All Rights Reserved.
 * www.csjbot.com
 * <p>
 * Created by 浦耀宗 at 2017/02/09 0009-11:10.
 * Email: puyz@csjbot.com
 */

class UartConnect implements IConnector {
    private UartComm UC = new UartComm();
    private int uart_fd = 0;
    private DataReceive uartDataReceive;
    private Thread UartReadThread;
    private boolean isRunning = false;

    /**
     * 连接服务器
     *
     * @param device   要打开的设备
     * @param baudrate 波特率
     */
    @Override
    public int connect(String device, int baudrate) {
        uart_fd = UC.uartInit(device);
        // TODO 判断ret
        int ret = UC.setOpt(uart_fd, baudrate, 8, 0, 1);
        if (ret != 0) {
            Csjlogger.error("upbody --- uart_fd " + uart_fd + " ret " + ret);
            return ret;
        }

        isRunning = true;
        UartReadThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int[] buffer = new int[256];
                while (isRunning) {
                    int size = UC.recv(uart_fd, buffer, 256);
                    if (size > 0) {
                        byte[] recData = new byte[size];
                        for (int i = 0; i < size; i++) {
                            recData[i] = (byte) buffer[i];
                        }

                        if (null != uartDataReceive) {
                            uartDataReceive.onReceive(recData);
                        }
                    } //end if(size != 0)
                }

                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "UartReadThread");

        UartReadThread.start();
        return ret;
    }


    /**
     * 发送数据
     *
     * @param data 要发送的二进制数据
     */
    @Override
    public int sendData(byte[] data) {
        if (uart_fd == 0) {
            // TODO 错误处理
            return 1;
        }

        int size = data.length;
        if (size <= 0) {
            return 2;
        }
        int[] sendData = new int[size];
        for (int i = 0; i < size; i++) {
            sendData[i] = data[i] & 0xff;
        }

        Csjlogger.info("sendData {}", Arrays.toString(data));
        return UC.send(uart_fd, sendData, size);
    }

    /**
     * 接受数据的回调设置
     *
     * @param receive 接收数据的回调
     * @see DataReceive
     */
    @Override
    public void setDataReceive(DataReceive receive) {
        this.uartDataReceive = receive;
    }

    /**
     * IConnector 是否连接
     *
     * @return 返回true则已经连接，返回false则没有连接
     */
    @Override
    public boolean isRunning() {
        return isRunning;
    }

    /**
     * 销毁 IConnector， 参见各个实现，保证所有的都设置为空
     */
    @Override
    public void destroy() {
        isRunning = false;
    }
}
